/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.util;

import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.async.LoadAndroidPhotoTask;
import com.phonebooksharing.android.widgets.drawable.FastBitmapDrawable;
import com.phonebooksharing.dto.friend.ContactItem;

public class ContactUtils {
    
    public static void setContactBorder(ContactItem c, View border) {
        if(c.isMyContact) {
            border.setBackgroundResource(R.drawable.detail_photo_border_blue);
        } else if (c.hasPbsAccount) {
            border.setBackgroundResource(R.drawable.detail_photo_border_yellow);
        } else {
            border.setBackgroundResource(R.drawable.detail_photo_border);
        }
    }
    
    public static void setContactPhoto(ExpandableListView elv, ContactItem c, ImageView iv,
                                       View border, int groupPosition, int childPosition) {
        final FastBitmapDrawable icon = GalleryUtilities.getFromContactCache(c.andID);
        if(icon != null) {
            iv.setImageBitmap(icon.getBitmap());
        } else {
            iv.setImageBitmap(GalleryUtilities.noPictureBitmap.getBitmap());
            long andID;
            if((andID = c.andID) > 0) {
                new LoadAndroidPhotoTask(elv).execute(andID, groupPosition, childPosition);
            } else {//TODO: nice to have later
//                new LoadNetworkImageTask(elv).execute(c.iconID, 60, 60, groupPosition, childPosition);
            }
        }
        setContactBorder(c, border);
    }
    
    public static void setContactPhoto(ListView lv, ContactItem c, ImageView iv, View border, int position) {
        final FastBitmapDrawable icon = GalleryUtilities.getFromContactCache(c.andID);
        if (icon != null) {
            iv.setImageBitmap(icon.getBitmap());
        } else {
            iv.setImageBitmap(GalleryUtilities.noPictureBitmap.getBitmap());
            long andID;
            if ((andID = c.andID) > 0) {
                new LoadAndroidPhotoTask(lv).execute(andID, position);
            } else {//TODO: nice to have later
//                new LoadNetworkImageTask(lv).execute(c.iconID, 60, 60, position, true /*isThumbnail*/, R.id.icon);
            }
        }
        setContactBorder(c, border);
    }
}
